<?php

namespace App\Services\GoldStormTimer;

use App\Models\GoldRound;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;

class GoldStormTimer
{
	protected Collection $rounds;

	public function __construct()
	{
		$this->setRounds();
	}

	protected function setRounds()
	{
		$this->rounds = GoldRound::all();
	}

    protected function robotReclaim(GoldRound $round)
    {
        if (cs('storm')->is_reclaim_on
            and $round->is_reclaiming) {
            reclaimer($round)->reclaim();
        }
    }

    protected function robotTerminate(GoldRound $round)
    {
        if (cs('storm')->is_terminate_on
            and $round->is_terminating) {
            Log::info("===== Start terminate robot");
            terminator($round)->terminate();
        }
    }

	public function __invoke()
	{
		if ($this->rounds->isEmpty()) return;

        /**
         * @var GoldRound $round
         */
		foreach ($this->rounds as $round) {
            if ($round->is_after_today_start
                and cs('storm')->is_robot_on) {
                $this->robotReclaim($round);
                $this->robotTerminate($round);
            }
		}
	}
}
